const http = require('http');

function makeRequest(options, data) {
  return new Promise((resolve, reject) => {
    const req = http.request(options, (res) => {
      let body = '';
      res.on('data', (chunk) => {
        body += chunk;
      });
      res.on('end', () => {
        try {
          resolve(JSON.parse(body));
        } catch (e) {
          resolve(body);
        }
      });
    });
    
    req.on('error', reject);
    
    if (data) {
      req.write(JSON.stringify(data));
    }
    req.end();
  });
}

async function testUrlValidation() {
  console.log('🔗 测试TikTok Shop URL格式验证');
  console.log('=' .repeat(50));
  console.log('');
  
  const testCases = [
    {
      name: '❌ 缺少所有参数',
      url: 'https://shop.tiktok.com/view/product/1731808168774042656',
      expectSuccess: false
    },
    {
      name: '❌ 只有region参数',
      url: 'https://shop.tiktok.com/view/product/1731808168774042656?region=MY',
      expectSuccess: false
    },
    {
      name: '❌ 缺少source参数',
      url: 'https://shop.tiktok.com/view/product/1731808168774042656?region=MY&local=en',
      expectSuccess: false
    },
    {
      name: '❌ 缺少local参数',
      url: 'https://shop.tiktok.com/view/product/1731808168774042656?region=MY&source=product_detail',
      expectSuccess: false
    },
    {
      name: '❌ 缺少region参数',
      url: 'https://shop.tiktok.com/view/product/1731808168774042656?local=en&source=product_detail',
      expectSuccess: false
    },
    {
      name: '✅ 完整正确的URL',
      url: 'https://shop.tiktok.com/view/product/1731808168774042656?region=MY&local=en&source=product_detail',
      expectSuccess: true
    },
    {
      name: '✅ 不同region的URL',
      url: 'https://shop.tiktok.com/view/product/1731808168774042656?region=US&local=en&source=product_detail',
      expectSuccess: true
    },
    {
      name: '❌ 非TikTok Shop链接',
      url: 'https://example.com/product/123',
      expectSuccess: false
    }
  ];
  
  for (let i = 0; i < testCases.length; i++) {
    const testCase = testCases[i];
    console.log(`📊 测试 ${i + 1}: ${testCase.name}`);
    console.log(`   🔗 URL: ${testCase.url}`);
    
    try {
      const options = {
        hostname: 'localhost',
        port: 3000,
        path: '/api/products',
        method: 'POST',
        headers: { 'Content-Type': 'application/json' }
      };
      
      const result = await makeRequest(options, { url: testCase.url });
      
      const actualSuccess = result.success || (result.message && result.message.includes('商品已存在'));
      
      if (actualSuccess === testCase.expectSuccess) {
        console.log('   ✅ 验证结果符合预期');
      } else {
        console.log('   ❌ 验证结果不符合预期');
        console.log(`   预期: ${testCase.expectSuccess ? '成功' : '失败'}, 实际: ${actualSuccess ? '成功' : '失败'}`);
      }
      
      if (!actualSuccess && result.message) {
        console.log(`   💬 错误信息: ${result.message}`);
        if (result.example) {
          console.log(`   📝 示例格式: ${result.example}`);
        }
      }
      
    } catch (error) {
      console.log('   ❌ 请求失败:', error.message);
    }
    
    console.log('');
  }
  
  console.log('🎉 URL格式验证测试完成！');
  console.log('=' .repeat(50));
  console.log('');
  console.log('📋 TikTok Shop URL格式要求:');
  console.log('');
  console.log('✅ 正确格式:');
  console.log('https://shop.tiktok.com/view/product/{PRODUCT_ID}?region={REGION}&local={LANGUAGE}&source={SOURCE}');
  console.log('');
  console.log('📝 参数说明:');
  console.log('• region: 地区代码 (MY, US, SG, TH, VN, PH, etc.)');
  console.log('• local: 语言代码 (en, zh, ms, th, vi, etc.)');
  console.log('• source: 来源标识 (product_detail, search, etc.)');
  console.log('');
  console.log('🌍 常用地区组合:');
  console.log('• 马来西亚: ?region=MY&local=en&source=product_detail');
  console.log('• 美国: ?region=US&local=en&source=product_detail');
  console.log('• 新加坡: ?region=SG&local=en&source=product_detail');
  console.log('• 泰国: ?region=TH&local=th&source=product_detail');
  console.log('• 越南: ?region=VN&local=vi&source=product_detail');
}

// 运行测试
testUrlValidation();